﻿using System;
using System.Data;
using System.Text;
using System.Drawing;
using System.ComponentModel;
using System.Collections.Generic;
using System.Linq;

using Gizmox.WebGUI.Common;
using Gizmox.WebGUI.Forms;
using Gizmox.WebGUI.Common.Resources;

namespace VisualWebGuiApplication2
{
    [Serializable()]
    public partial class ItemPanel : UserControl
    {
		public Gizmox.WebGUI.Forms.ListView mobjListView;
		private Gizmox.WebGUI.Forms.ColumnHeader ColumnAmount;
		private Gizmox.WebGUI.Forms.ColumnHeader ColumnSubject;
		private Gizmox.WebGUI.Forms.ColumnHeader ColumnNote;
        private Gizmox.WebGUI.Forms.ColumnHeader ColumnDate;
        private int Index = 0;
		private RandomData mobjRandomData = null;
        public BindingList<FixOperation> _listofFixOperation;
        public BindingList<TankFixOperation> _listofTankFixOperation;

		public ItemPanel()
		{
			// This call is required by the WebGUI Form Designer.
			InitializeComponent();
            mobjListView.View = View.Details;


          
		
           
		}
        public ItemPanel(int TankFixID)
        {
            // This call is required by the WebGUI Form Designer.
            InitializeComponent();
            mobjListView.View = View.Details;
            bool newoperations=true;

            if (TankFixID >0 )
            {
                using (GazOnDevEntities db = new GazOnDevEntities())
                {
                    try
                    {
                        var query = from p in db.TankFixOperations
                                    where p.tankID == TankFixID
                                    orderby p.id ascending
                                    select p;
                        _listofTankFixOperation = new BindingList<TankFixOperation>(query.ToList<TankFixOperation>());
                    }catch(Exception expp){}

                }
                    
               
                    if (_listofTankFixOperation.Count > 0)
                    {

                        newoperations = false;
                    }
                    else
                    {

                        newoperations = true;
                    }
                
                
            }
            else
            {
               
                    using (GazOnDevEntities db = new GazOnDevEntities())
                    {
                        var query = from p in db.FixOperations
                                    orderby p.oreder ascending
                                    select p;
                        _listofFixOperation = new BindingList<FixOperation>(query.ToList<FixOperation>());


                    }
                 
            }
            if (newoperations)
            {
                using (GazOnDevEntities db = new GazOnDevEntities())
                {
                    var query = from p in db.FixOperations
                                orderby p.oreder ascending
                                select p;
                    _listofFixOperation = new BindingList<FixOperation>(query.ToList<FixOperation>());


                }
                FixOperationGenerateNew();
            }

            else
            {

                FixOperationGenerateEdit();
            }

        }


        /// <summary>
        /// Adds row to ListView
        /// </summary>
		private void FixOperationGenerateNew()
		{           
            foreach (FixOperation item in _listofFixOperation)
            {
                ListViewControlPanel objPanel = new ListViewControlPanel();
                ListViewItem objItem = null;
                objPanel.Visible = false;
                objItem = this.mobjListView.Items.Add(objPanel,item.Subject);
                if ((Index % 2) == 0)
                {
                    objItem.BackColor = Color.AntiqueWhite;
                }
                else
                {
                    objItem.BackColor = Color.Azure;
                }

                Index++;
                objItem.SubItems.Add(objPanel.Amount );
                objItem.SubItems.Add(objPanel.DateOfOperaTion );
                objItem.SubItems.Add(objPanel.Note );
               
                //objItem.SubItems.Add(objPanel.Amount =item);
                //objItem.SubItems.Add(objPanel.DateOfOperaTion = DateTime.Now.ToString("MM/dd/yyyy"));
                //objItem.SubItems.Add(objPanel.Note = "הערות");
                Gizmox.WebGUI.Forms.Button objButton = new Gizmox.WebGUI.Forms.Button();
                objButton.Click += new EventHandler(OnEditButtonClick);
                objButton.Tag = objItem;
                objButton.Text = "עריכה";
                objItem.SubItems.Add(objButton);
            }
			
                
			
		}
        private void FixOperationGenerateEdit()
        {
            foreach (TankFixOperation item in _listofTankFixOperation)
            {
                ListViewControlPanel objPanel = new ListViewControlPanel();
                ListViewItem objItem = null;
                objPanel.Visible = false;
                objItem = this.mobjListView.Items.Add(objPanel, item.title);
                if ((Index % 2) == 0)
                {
                    objItem.BackColor = Color.AntiqueWhite;
                }
                else
                {
                    objItem.BackColor = Color.Azure;
                }

                Index++;
                objItem.SubItems.Add(objPanel.Amount =item.amount.ToString());
                if (null != item.date)
                {
                    objItem.SubItems.Add(objPanel.DateOfOperaTion = Convert.ToDateTime(item.date).ToString("dd/MM/yyyy"));
                }
                else
                { 
                      objItem.SubItems.Add(objPanel.DateOfOperaTion =string.Empty);
                
                }
                objItem.SubItems.Add(objPanel.Note = item.note);
                //objItem.SubItems.Add(objPanel.Amount =item);
                //objItem.SubItems.Add(objPanel.DateOfOperaTion = DateTime.Now.ToString("MM/dd/yyyy"));
                //objItem.SubItems.Add(objPanel.Note = "הערות");
                Gizmox.WebGUI.Forms.Button objButton = new Gizmox.WebGUI.Forms.Button();
                objButton.Click += new EventHandler(OnEditButtonClick);
                objButton.Tag = objItem;
                objButton.Text = "עריכה";
                objItem.SubItems.Add(objButton);
            }



        }
        /// <summary>
        /// Handles click event of the Button that is in action column in ListView.
        /// The method opens panel for editing row data and after editting save changed data
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnEditButtonClick(object sender, EventArgs e)
        {
            Gizmox.WebGUI.Forms.Button objButton = sender as Gizmox.WebGUI.Forms.Button;
            if (objButton != null)
            {
                ListViewPanelItem objPanelItem = objButton.Tag as ListViewPanelItem;
                if (objPanelItem != null)
                {
                    if (objPanelItem.Panel.Visible = !objPanelItem.Panel.Visible)
                    {
                        objButton.Text = "שמירה";
                    }
                    else
                    {
                        objButton.Text = "עריכה";

                        ListViewControlPanel objPanel = objPanelItem.Panel as ListViewControlPanel;
                        if (objPanel != null)
                        {
                            //if (objPanel.Amount)
                            //{
                            //    objPanelItem.SubItems[0].Text = GetIcon("ImportantMail.gif");
                            //}
                            //else
                            //{
                            //    objPanelItem.SubItems[0].Text = "";
                            //}

                            //if (objPanel.Read)
                            //{
                            //    objPanelItem.SubItems[1].Text = GetIcon("OpenedMail.gif");
                            //}
                            //else
                            //{
                            //    objPanelItem.SubItems[1].Text = GetIcon("ClosedMail.gif");
                            //}

                            //if (objPanel.Attachemnts)
                            //{
                            //    objPanelItem.SubItems[2].Text = GetIcon("AttachedMail.gif");
                            //}
                            //else
                            //{
                            //    objPanelItem.SubItems[2].Text = "";
                            //}
                            if (!objPanel.usevalues)
                            {

                                objPanelItem.SubItems[1].Text = objPanel.Amount;

                                objPanelItem.SubItems[2].Text = objPanel.DateOfOperaTion;
                                objPanelItem.SubItems[3].Text = objPanel.Note;
                            }
                            else
                            {
                                objPanelItem.SubItems[1].Text = string.Empty;

                                objPanelItem.SubItems[2].Text = string.Empty;
                                objPanelItem.SubItems[3].Text = "לא ישים"; 
                            }
                        }
                    }
                }
            }
        }
        /// <summary>
        /// Gets resource string for specified icon name
        /// </summary>
        /// <param name="strName">icon name</param>
        /// <returns></returns>
		private string GetIcon(string strName)
		{
			return (new IconResourceHandle(strName)).ToString();
		}

        [Serializable()]
        public class RandomData
        {
            private Random mobjRandom;

            public RandomData()
            {
                mobjRandom = new Random(0);
            }

            public bool GetBoolean()
            {
                return mobjRandom.NextDouble() > 0.5;
            }

            public string GetSize()
            {
                return mobjRandom.Next(1, 2000).ToString() + "kb";
            }

            public DateTime GetDate()
            {
                return DateTime.Now.AddDays((double)mobjRandom.Next(-100, 100));
            }
        }
    }
}